home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / tusrc.zip / SRC / WC.C < prev   
C/C++ Source or Header  |  1993-10-02  |  6KB  |  263 lines

  1. /* wc - print the number of bytes, words, and lines in files
  2.    Copyright (C) 1985, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Paul Rubin, phr@ocf.berkeley.edu
  19.    and David MacKenzie, djm@gnu.ai.mit.edu. */
  20.  
  21. #include <stdio.h>
  22. #include "../lib/getopt.h"
  23. #include <sys/types.h>
  24. #include <io.h>
  25. #include "system.h"
  26. #include "version.h"
  27.  
  28. /* Size of atomic reads. */
  29. #define BUFFER_SIZE (16 * 1024)
  30.  
  31. void error ();
  32.  
  33. static void wc ();
  34. static void wc_file ();
  35. static void write_counts ();
  36.  
  37. /* The name this program was run with. */
  38. char *program_name;
  39.  
  40. /* Cumulative number of lines, words, and chars in all files so far. */
  41. static unsigned long total_lines, total_words, total_chars;
  42.  
  43. /* Which counts to print. */
  44. static int print_lines, print_words, print_chars;
  45.  
  46. /* Nonzero if we have ever read the standard input. */
  47. static int have_read_stdin;
  48.  
  49. /* The error code to return to the system. */
  50. static int exit_status;
  51.  
  52. /* If non-zero, display usage information and exit.  */
  53. static int flag_help;
  54.  
  55. /* If non-zero, print the version on standard error.  */
  56. static int flag_version;
  57.  
  58. static struct option const longopts[] =
  59. {
  60.   {"bytes", no_argument, NULL, 'c'},
  61.   {"chars", no_argument, NULL, 'c'},
  62.   {"lines", no_argument, NULL, 'l'},
  63.   {"words", no_argument, NULL, 'w'},
  64.   {"help", no_argument, &flag_help, 1},
  65.   {"version", no_argument, &flag_version, 1},
  66.   {NULL, 0, NULL, 0}
  67. };
  68.  
  69. static void
  70. usage ()
  71. {
  72.   fprintf (stderr, "\
  73. Usage: %s [-clw] [--bytes] [--chars] [--lines] [--words]\n\
  74.        [--help] [--version] [file...]\n",
  75.        program_name);
  76.   exit (1);
  77. }
  78.  
  79. void
  80. main (argc, argv)
  81.      int argc;
  82.      char **argv;
  83. {
  84.   int optc;
  85.   int nfiles;
  86.  
  87.   program_name = argv[0];
  88.   exit_status = 0;
  89.   print_lines = print_words = print_chars = 0;
  90.   total_lines = total_words = total_chars = 0;
  91.  
  92.   while ((optc = getopt_long (argc, argv, "clw", longopts, (int *) 0)) != EOF)
  93.     switch (optc)
  94.       {
  95.       case 0:
  96.     break;
  97.  
  98.       case 'c':
  99.     print_chars = 1;
  100.     break;
  101.  
  102.       case 'l':
  103.     print_lines = 1;
  104.     break;
  105.  
  106.       case 'w':
  107.     print_words = 1;
  108.     break;
  109.  
  110.       default:
  111.     usage ();
  112.       }
  113.  
  114.   if (flag_version)
  115.     {
  116.       fprintf (stderr, "%s\n", version_string);
  117.       exit (0);
  118.     }
  119.  
  120.   if (flag_help)
  121.     usage ();
  122.  
  123.   if (print_lines + print_words + print_chars == 0)
  124.     print_lines = print_words = print_chars = 1;
  125.  
  126.   nfiles = argc - optind;
  127.  
  128.   if (nfiles == 0)
  129.     {
  130.       have_read_stdin = 1;
  131.       wc (0, "");
  132.     }
  133.   else
  134.     {
  135.       for (; optind < argc; ++optind)
  136.     wc_file (argv[optind]);
  137.  
  138.       if (nfiles > 1)
  139.     write_counts (total_lines, total_words, total_chars, "total");
  140.     }
  141.  
  142.   if (have_read_stdin && close (0))
  143.     error (1, errno, "-");
  144.  
  145.   exit (exit_status);
  146. }
  147.  
  148. static void
  149. wc_file (file)
  150.      char *file;
  151. {
  152.   if (!strcmp (file, "-"))
  153.     {
  154.       have_read_stdin = 1;
  155.       wc (0, file);
  156.     }
  157.   else
  158.     {
  159.       int fd = open (file, O_RDONLY);
  160.       if (fd == -1)
  161.     {
  162.       error (0, errno, "%s", file);
  163.       exit_status = 1;
  164.       return;
  165.     }
  166.       wc (fd, file);
  167.       if (close (fd))
  168.     {
  169.       error (0, errno, "%s", file);
  170.       exit_status = 1;
  171.     }
  172.     }
  173. }
  174.  
  175. static void
  176. wc (fd, file)
  177.      int fd;
  178.      char *file;
  179. {
  180.   char buf[BUFFER_SIZE];
  181.   register int bytes_read;
  182.   register int in_word = 0;
  183.   register unsigned long lines, words, chars;
  184.   struct stat stats;
  185.  
  186.   lines = words = chars = 0;
  187.  
  188.   if (print_chars && !print_words && !print_lines
  189.       && fstat (fd, &stats) == 0 && S_ISREG (stats.st_mode))
  190.     {
  191.       chars = stats.st_size;
  192.     }
  193.   else
  194.     {
  195.       while ((bytes_read = read (fd, buf, BUFFER_SIZE)) > 0)
  196.     {
  197.       register char *p = buf;
  198.  
  199.       chars += bytes_read;
  200.       do
  201.         {
  202.           switch (*p++)
  203.         {
  204.         case '\n':
  205.           lines++;
  206.           /* Fall through. */
  207.         case '\r':
  208.         case '\f':
  209.         case '\t':
  210.         case '\v':
  211.         case ' ':
  212.           if (in_word)
  213.             {
  214.               in_word = 0;
  215.               words++;
  216.             }
  217.           break;
  218.         default:
  219.           in_word = 1;
  220.           break;
  221.         }
  222.         }
  223.       while (--bytes_read);
  224.     }
  225.       if (bytes_read < 0)
  226.     {
  227.       error (0, errno, "%s", file);
  228.       exit_status = 1;
  229.     }
  230.       if (in_word)
  231.     words++;
  232.     }
  233.  
  234.   write_counts (lines, words, chars, file);
  235.   total_lines += lines;
  236.   total_words += words;
  237.   total_chars += chars;
  238. }
  239.  
  240. static void
  241. write_counts (lines, words, chars, file)
  242.      unsigned long lines, words, chars;
  243.      char *file;
  244. {
  245.   if (print_lines)
  246.     printf ("%7lu", lines);
  247.   if (print_words)
  248.     {
  249.       if (print_lines)
  250.     putchar (' ');
  251.       printf ("%7lu", words);
  252.     }
  253.   if (print_chars)
  254.     {
  255.       if (print_lines || print_words)
  256.     putchar (' ');
  257.       printf ("%7lu", chars);
  258.     }
  259.   if (*file)
  260.     printf (" %s", file);
  261.   putchar ('\n');
  262. }
  263.